﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Avatar.Common
{
    public static class Helper
    {
        /// <summary>
        /// 获取用户IP地址
        /// </summary>
        /// <returns></returns>
        public static string GetClientIP()
        {
            if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
            {
                return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else
            {
                return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            }
        }
        /// <summary>
        /// 显示信息
        /// </summary>
        /// <param name="_errStr"></param>
        /// <param name="_goto"></param>
        public static void ShowMassage(string _msgStr, string _gotoURL)
        {
            if (string.IsNullOrEmpty(_msgStr)) throw new Exception("信息内容不能为空");
            if (string.IsNullOrEmpty(_gotoURL)) throw new Exception("转向页面地址不能为空");

            HttpContext.Current.Response.Write("<script>alert('" + _msgStr + "');window.location='" + _gotoURL + "';</script>");
        }
        /// <summary>
        /// 显示错误信息
        /// </summary>
        /// <param name="_errStr"></param>
        public static void ShowMassage(string _msgStr)
        {
            if (string.IsNullOrEmpty(_msgStr)) throw new Exception("信息内容不能为空");

            HttpContext.Current.Response.Write("<script>alert('" + _msgStr + "');</script>");
        }

        /// <summary>
        /// 获取URL被加密后的键值对字典
        /// </summary>
        /// <param name="_url"></param>
        /// <returns></returns>
        public static Dictionary<string, string> RequestEncodeUrlParamaters(string _url)
        {
            var _uri =HttpContext.Current.Server.UrlDecode(_url);
            if (_uri.IndexOf("?") == 0) _uri = _uri.Replace("?", "");

            var _parm = _uri.Split(new Char[] { '&' });

            Dictionary<string, string> _d = new Dictionary<string, string>();
            foreach (var _i in _parm)
            {
                _d.Add(_i.Substring(0, _i.LastIndexOf('=')), _i.Substring(_i.IndexOf("=") + 1));
            }

            return _d;
        }
        public static string DateTimeStringFormat(DateTime _date)
        {
            string _dateStr = string.Empty;


            if (_date.ToShortDateString() == DateTime.Today.ToShortDateString())
            {
                //是今天
                _dateStr = _date.Hour.ToString() + ":" + _date.Minute.ToString();
            }
            else
            {
                //不是今天
                if (_date.Year != DateTime.Now.Year)
                {
                    //不是今年
                    _dateStr = _date.Year.ToString() + "-" + _date.Month.ToString() + "-" + _date.Day.ToString();
                }
                else
                {
                    //是今年
                    var _month = string.Empty;
                    if (_date.Month != DateTime.Now.Month)
                    {
                        //不是当月
                        _dateStr += _date.Month.ToString() + "-";
                    }
                    else
                    {
                        //是当月
                        _dateStr += "本月";
                    }
                    _dateStr += _date.Day.ToString()+"日";
                }
            }
            return _dateStr;
        }
    }
}
